<?php
defined('_JEXEC') OR die('Restricted access');
global $mainframe;
require_once(JApplicationHelper::getPath('admin_html'));

//fetch parameters
$id = JRequest::getVar('id', 0, 'get', 'int');
$cid = JRequest::getVar('cid', array(0), 'post', 'array');

$task = JRequest::getVar('task', '');
switch($task) {
case 'listhotel':
    listHotel($option);
    break;
case 'add':
	newHotel($option);
	break;
case 'edit':
    editHotel($option, $cid);
    break;
case 'save':
    createHotel($option);
    break;
case 'remove':
    removeHotel($option, $cid);
    break;
case 'update':
    updateHotel($option);
    break;
default:
	listHotel($option);
}

function listHotel($option) 
{
    jimport('smarty.smarty');    
    $db = JFactory::getDBO();
    jimport('hotel.business.HotelBusiness');
    $hobj =& HotelBusiness::getInstance($db);
    $details = $hobj->listHotel();    
    $smarty = new JSmarty();
    $smarty->template_dir = getCompDir() . DS . 'templates';
    HTML_hotel::displayHotelList($smarty, $details, $option);    
}

function newHotel($option)
{
    jimport('smarty.smarty');
    jimport('hotel.database');
    jimport('hotel.model.HHotelDetail');
    $smarty = new JSmarty();
    $smarty->template_dir = getCompDir() . DS . 'templates';
    $db = JFactory::getDBO();
    $details = new HHotelDetail($db);
    HTML_hotel::displayHotelForm($smarty, $details, $option);
}

function createHotel($option) {
	$params = array();
    $params['name'] = JRequest::getVar('name', '');
    $params['alias'] = JRequest::getVar('alias', '');
    $params['description'] = JRequest::getVar('desc', '');
    $params['Language_ID'] = 1;
    $params['UUID'] = '01' . uniqid(rand());
    jimport('hotel.model.HHotel');
    jimport('hotel.model.HHotelDetail');
    jimport('hotel.business.HotelBusiness');
    $db = JFactory::getDBO();
    $hobj = HotelBusiness::getInstance($db);
	if(!$hobj->addNewHotel($params)) return false;    
    global $mainframe;
    $mainframe->redirect("index.php?option=$option");
}

function editHotel($option, $cid){
    jimport('hotel.business.HotelBusiness');
    jimport('smarty.smarty');
    $db = JFactory::getDBO();
    
    $hobj =& HotelBusiness::getInstance($db);
    $detail =& $hobj->getHotel($cid[0]);
    
    $smarty = new JSmarty();
    $smarty->template_dir = getCompDir() . DS . 'templates';
    HTML_hotel::displayHotelForm($smarty, $detail, $option);
}

function updateHotel($option) {
    jimport('hotel.business.HotelBusiness');
    $db = JFactory::getDBO();
    $hobj =& HotelBusiness::getInstance($db);
    $params = array();
    $params['Hotel_ID'] = JRequest::getVar('id');
    $params['name'] = JRequest::getVar('name');
    $params['alias'] = JRequest::getVar('alias');
    $params['description'] = JRequest::getVar('desc');
    $params['Language_ID'] = 1;
    if(!$hobj->updateHotel($params)) {
        return false;
    }
    global $mainframe;
    $mainframe->redirect("index.php?option=$option");
}

function removeHotel($option, $cid) {
	jimport('hotel.business.HotelBusiness');
	global $mainframe;
	$db =& JFactory::getDBO();
	$hobj =& HotelBusiness::getInstance($db);
	if(!$hobj->removeHotel($cid)){
	    return false;
	}
    $mainframe->redirect("index.php?option=$option");
}

function getCompDir() {
    $path = JApplicationHelper::getPath('class');
    $parts = explode(DS, $path);
    array_pop($parts);
    $path = implode(DS, $parts);
    return $path;
}

?>